import 'package:floor/floor.dart';
import 'package:share_fit/app/models/base_model.dart';
import 'package:share_fit/app/tools/date_tool.dart';

/// 海拔
@Entity(tableName: 'sports_altitude')
class AltitudeModel extends BaseModel {
  /// 运动模块产生的运动记录id
  @ColumnInfo(name: 'sport_id')
  late int sportId;

  late int altitude;

  /// 日期时间，格式 ：yyyy-MM-dd HH:mm
  @ColumnInfo(name: 'calendar_time')
  late String? calendarTime;

  /// 日期，格式 ：yyyyMMdd
  late String? calendar;

  @override
  bool from(Map map) {
    try {
      var temp = map['totalAltitude'] ?? (map['altitude'] ?? 0);

      // android字段名称
      var temp1 = map['elevation'];
      if (temp == null && temp1 == null) {
        return false;
      }

      altitude = temp ?? temp1;
      if (map['timeInfo'] > 0) {
        DateTime dt = DateTool.getDateTimeFrom(map['timeInfo']);
        String s = DateTool.getYMDHMWithDash(dt);
        calendarTime = s;
        calendar = s.split(' ').first;
      }
      return true;
    } catch (e) {
      return false;
    }
  }

  @override
  toMap() {
    return {
      'sportId': sportId,
      'altitude': altitude,
      'calendarTime': calendarTime,
      'calendar': calendar,
    };
  }

  @override
  List<Object?> get props => [altitude, calendarTime, calendar];

  @override
  bool? get stringify => true;
}
